function I = imagePositionTransformer(I, spatialRotation, xPatternOffset, yPatternOffset, imageXrange, imageYrange)
% imagePositionTransformer
%
% de-offsets and de-rotates and image
%
% Editing:
% gs may 2006
% -----------------------------------------------------------

% DE-OFFSET
Hlims = [-imageXrange/2 imageXrange/2];
Vlims = [-imageYrange/2 imageYrange/2];
[Vsize, Hsize] = size(I);

% horizontal
HmicronsPerPixel = (max(Hlims) - min(Hlims)) / Hsize;
HoffsetInPixels = abs(round(xPatternOffset / HmicronsPerPixel));
HblankPart = zeros(Vsize, HoffsetInPixels);
if sign(xPatternOffset) == 1
    % shift upwards
    Itrimmed = I(:, HoffsetInPixels+1:end);
    I = [Itrimmed, HblankPart];
elseif sign(xPatternOffset) == -1
    % shift downwards
    Itrimmed = I(:, 1:end-HoffsetInPixels);
    I = [HblankPart, Itrimmed];
elseif sign(xPatternOffset) == 0
    % do nothing
end

% vertical
VmicronsPerPixel = (max(Vlims) - min(Vlims)) / Vsize;
VoffsetInPixels = abs(round(yPatternOffset / VmicronsPerPixel));
VblankPart = zeros(VoffsetInPixels, Hsize);
if sign(yPatternOffset) == -1
    Itrimmed = I(VoffsetInPixels+1:end, :);
    I = [Itrimmed; VblankPart];
elseif sign(yPatternOffset) == 1
    % shift downwards
    Itrimmed = I(1:end-VoffsetInPixels, :);
    I = [VblankPart; Itrimmed];
elseif sign(yPatternOffset) == 0
    % do nothing
end

% DE-ROTATE
I = imrotate(I, -spatialRotation, 'crop');

